import { observable, action } from 'mobx';
import { listStore } from '.';
import { getListMainKey, getListSubKey } from '../../util';
import { Modal, message } from 'antd';
import API from '../../apis';
import window from '../../util';
import {WeaLocaleProvider} from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export interface ListEditStoreProps {
    sessionKey: string;
    datasChange: (datas: any[]) => void;
    changedDatas: any[];
    editTablePaginationChange: (p: any, f: any, s: any) => void;
    editableColumns: any;
    back: () => void;
}
export class ListEditStore implements ListEditStoreProps {
    Search: listStore;
    constructor(Search) {
        this.Search = Search;
    }
    sessionKey = '';
    @observable changedDatas = [] as any[];
    @observable editableColumns = {};
    back = () => {
        window.weaHistory.goBack();
    }
    @action
    datasChange = (datas: any[]) => {
        const diffs = [] as any[];
        for (let i = 0; i < datas.length && i < this.Search.newTable.datas.length; i++) {
            let changed = datas.slice()[i];
            let data = this.Search.newTable.datas.slice()[i];
            let diff = {} as any;
            diff.notEmpty = false;
            for (let key in data) {
                if (!key.endsWith("span")) {
                    if (changed[key] != data[key]) {
                        diff[key] = changed[key];
                        diff.notEmpty = true;
                    }
                }
            }

            if (diff.notEmpty) {
                diff.primaryKey = getListMainKey(data);
                diff.subPrimaryKey = getListSubKey(data);
                diffs.push(diff);
            }
        }
        this.changedDatas = diffs;
    }
    editTablePaginationChange = (p: any, _: any, s: any) => {
        const callback = () => {
            this.Search.newTable.getDatas(this.sessionKey, p.current, p.pageSize, s);
        }
        if (this.changedDatas.length > 0) {
            Modal.confirm({
                title: getLabel(82873,'请确认'),
                content:getLabel(383745,'当前页面数据有修改，是否需要保存？点击【确认】保存当前页，点击【取消】直接跳转！'),
                okText: getLabel(16631, "确认"),
                cancelText: getLabel(201,"取消"),
                onOk: this.batchSave.bind(this, callback),
                onCancel: callback
            });
        } else {
            callback();
        }

    }
    @action
    batchSave = (callback: () => void=this.back) => {
        this.Search.newTable.loading = true;
        API.batchSave({ customid: this.Search.customId, diffs: JSON.stringify(this.changedDatas) }).then(({ status, error }) => {
            if (status == '1') {
                callback();
            } else {
                message.error(error);
                throw error;
            }
            this.Search.newTable.loading = false;
        })
        

    }

    @action
    update = (param) => {
        for (let key in param) {
            this[key] = param[key]
        }
    }
}